const mongoose = require('mongoose');
var Order = mongoose.model('order');
var Merchant = mongoose.model('merchant');
var User = mongoose.model('user');
const jms = require('../config/jms');
const webServiceConfig =  require('../WebServiceProperties');
var request = require('request');

exports.findUserOrders = function (req,res) {
    Order.find({user:mongoose.Types.ObjectId(req.query.uid)}).populate('merchant').populate('user').sort({'_id':-1}).select('-merchant.password -user.password -merchant.account -user.account').exec(function (err,orders) {
        if(!err){
            res.json(orders);
        }else{
            res.json({error: 'error in find all orders'});
        }
    })
}


exports.findOrderById = function (req,res) {
    Order.findOne({_id:req.params._id}).populate('merchant').populate('user').select('-merchant.password -user.password -merchant.account -user.account').exec(function (err,order) {
        if(!err){
            res.json(order);
        }else{
            res.json({error: 'error in find order by id'});
        }
    })
}


exports.putOrder = function (req,res) {

    Order.findOne({_id:req.params._id}).populate('merchant').populate('user').exec(function (err,order1) {
        if(!err){
            order1.update(req.body,function (err,order) {
                if(!err){
                    //投诉的话发JMS
                    if(req.body.comments!=undefined){
                        jms.toComplaintOrder({mid:order1.merchant._id,body:req.body.comments,oid:req.params._id});
                        Order.findOne({_id:req.params._id},function (err,order) {
                            if(!err){
                                res.json(order);
                            }else{
                                res.json({error: 'error find updated order in complaint order'});
                            }
                        })
                    }
                    //评分处理
                    else if(req.body.score!=undefined){
                        console.log(order1.merchant._id);
                        Merchant.findOne({_id: order1.merchant._id},function (err,merchant) {
                            if(!err){
                                merchant.star = (merchant.star + req.body.score*1)/2;
                                if(merchant.star<5&&merchant.orderNumber>101){
                                    var star1 = merchant.star;
                                    jms.ToBlackShop({mid: merchant._id,body:star1.toFixed(2),oid:req.params._id});
                                }
                                merchant.save(function (err) {
                                    if(err){
                                        res.json({error: 'error save merchant in score merchant'})
                                    }else{
                                        Order.findOne({_id:req.params._id},function (err,order) {
                                            if(!err){
                                                res.json(order);
                                            }else{
                                                res.json({error: 'error find order in score order'})
                                            }
                                        });
                                    }
                                })
                            }else{
                                res.json({error: 'error update merchant star in score order'})
                            }
                        })
                    }
                    else{
                        Order.findOne({_id:req.params._id},function (err,order) {
                            if(!err){
                                res.json(order);
                            }else{
                                res.json({error: 'error find updated order in put order'});
                            }
                        })
                    }
                }else{
                    res.json({error: 'error update order in putOrder'})
                }
            })
        }else{
            res.json({error: 'error  find order in putOrder'});
        }
    })
}


exports.addOrder = function (req,res) {

    Merchant.findOne({_id:req.session.mid},function(err,merchant){
        if(!err){
            User.findOne({_id: req.session.uid},function(err,user){
                if(!err){
                    var order = new Order(req.body);
                    order.merchant = merchant;
                    order.user = user;
                    order.orderTime = new Date();
                    order.save(function (err) {
                        if(err){
                            res.json({error: 'error save order in addOrder'});
                        }else{
                            merchant.orderNumber++;
                            merchant.save(function (err) {
                                if(err){
                                    res.json({error: 'error save merchant orderNumber in add order'});
                                }else{
                                    res.json(order);
                                }
                            })
                        }
                    })
                }else{
                    res.json({error: 'error find user in addOrder'})
                }
            });
        }else{
            res.json({error: 'error find merchant in addOrder'});
        }
    })

}

exports.getComplaintResult = function (req,res) {
    request(webServiceConfig.getComplaintResultUrl+req.query.oid, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            res.json(JSON.parse(body));
        }
    })
}



